package gnss

import (
	"fmt"
)

/**
  用户测距精度
    // 1061(GPS), 1067(GLO)
*/

type IgmasSsrUraSat struct {
	Satid uint8
	Ura   uint8
}

// DF389
/*
URA≤(3^URA_CLASS)(1+URA_VALUE/4)-1
式中：
URA：SSR用户等效距离，单位mm；
URA_CLASS：DF389的3MSB，无单位；
URA_VALUE：DF389的3LSB，单位mm。
DF389=000000（二进制）表示URA未定义/未知，相应卫星SSR数据无效；
DF389=111111（二进制）表示URA>5466.5mm。

SSR User Range Accuracy (URA) (1 sigma) for a range correction computed from complete SSR set as disseminated by RTCM SSR
messages. The URA is represented by a combination of URA_CLASS
and URA_VALUE. The 3 MSb define the URA_CLASS with a range
of 0-7 and the 3 LSb define the URA_VALUE with a range of 0-7.
The URA is computed by:
URA [mm] ≤ 3 ^ URA_CLASS (1 + URA_VALUE/4) − 1 [mm]
Special cases are:
000 000 - URA undefined/unknown, SSR corrections for the corresponding satellite may not be reliable
111 111 - URA > 5466.5 mm
*/

///* variance by ura ssr (ref [10] table 3.3-1 DF389) --------------------------*/
//  #define SQR(x)   ((x)*(x))
//  #define DEFURASSR 0.15            /* default accurary of ssr corr (m) */
//static double var_urassr(int ura)
//{
//double std;
//if (ura<= 0) return SQR(DEFURASSR);
//if (ura>=63) return SQR(5.4665);
//std=(pow(3.0,(ura>>3)&7)*(1.0+(ura&7)/4.0)-1.0)*1E-3;
//return SQR(std);
//}

func (this *IgmasSsrUraSat) String() string {
	return fmt.Sprintf("SatId:%d, Ura:%d", this.Satid, this.Ura)
}

func EncodeIgmasSsrUra(head *IgmasSsrCorrectionHead, lst []*IgmasSsrUraSat, outBuf []byte, startBitN int) (bitN int) {

	bitN = EncodeIgmasSsrHead(head, outBuf, startBitN, len(lst))

	// 67

	for i := 0; i < len(lst); i++ {
		itm := lst[i]
		SetBitU8(outBuf, bitN, 6, itm.Satid)
		bitN += 6

		SetBitU8(outBuf, bitN, 6, itm.Ura)
		bitN += 6
	}

	return
}

func DecodeIgmasSsrUra(inBuf []byte, startBitN int) (head *IgmasSsrCorrectionHead, lst []*IgmasSsrUraSat, bitN int) {
	head = &IgmasSsrCorrectionHead{}
	bitN, n := DecodeIgmasSsrHead(head, inBuf, startBitN)
	lst = make([]*IgmasSsrUraSat, n)
	for i := 0; i < len(lst); i++ {
		itm := &IgmasSsrUraSat{}
		lst[i] = itm
		itm.Satid = GetBitU8(inBuf, bitN, 6)
		bitN += 6

		itm.Ura = GetBitU8(inBuf, bitN, 6)
		bitN += 6
	}

	return
}
